<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row>
        <el-col>
          <el-button
            type="primary"
            @click="ShowAddCate"
          >添加分类</el-button>
        </el-col>
      </el-row>
      <el-row class="rolelist">
        <!-- 表格 -->
        <tree-table
          :data="catelist"
          :columns="columns"
          :selection-type="false"
          :expand-type="false"
          show-index
          index-text="#"
          border
          :show-row-hover="false"
        >
          <!-- 是否有效 -->
          <template
            slot="isok"
            slot-scope="scope"
          >
            <i
              style="color: lightgreen;"
              v-if="scope.row.cat_deleted === false"
              class="el-icon-success"
            ></i>
            <i
              style="color: red;"
              v-else
              class="el-icon-error"
            ></i>
          </template>
          <!-- 排序 -->
          <template
            slot="order"
            slot-scope="scope"
          >
            <el-tag
              v-if="scope.row.cat_level === 0"
              size="mini"
            >一级</el-tag>
            <el-tag
              v-else-if="scope.row.cat_level === 1"
              type="success"
              size="mini"
            >二级</el-tag>
            <el-tag
              v-else
              type="warning"
              size="mini"
            >三级</el-tag>
          </template>
          <!-- 操作 -->
          <template
            slot="opt"
            slot-scope="scope"
          >
          <!-- 取消错误 -->
          <p style="display: none;">{{scope.row}}</p>
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
            >编辑</el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
            >删除</el-button>
          </template>
        </tree-table>
        <!-- 分页 -->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[ 3, 5, 10, 15]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 添加分类弹框 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="30%"
    >
      <!-- 添加分类表单 -->
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="addCateFormRef"
        label-width="100px"
      >
        <el-form-item
          label="分类名称："
          prop="cat_name"
        >
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类：">
          <!-- options数据源 -->
          <!-- props指定数据对象 -->
          <el-cascader
            v-model="selectedKeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="parentCateChange"
            clearable
            change-on-select
            @close="addCateDialogClose"
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addCate"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      //商品分类数据列表
      catelist: [],
      //查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //总页数
      total: 1,
      //指定列信息
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok",
        },
        {
          label: "排序",
          type: "template",
          template: "order",
        },
        {
          label: "操作",
          type: "template",
          template: "opt",
        },
      ],
      // 控制添加分类对话框
      addCateDialogVisible: false,
      //添加分类的表单数据对象
      addCateForm: {
        cat_name: "",
        cat_id: 0,
        cat_level: 0,
      },
      //验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //父级分类列表
      parentCateList: [],
      cascaderProps: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //选中的父级分类的id数组
      selectedKeys: [],
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("/categories", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200)
        return this.$message.error("获取商品分类列表失败");
      // console.log(res.data.result);
      //拿到数据列表
      this.catelist = res.data.result;
      this.total = res.data.total;
    },
    //监听pagesize大小
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    //监听pagenum变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    ShowAddCate() {
      this.addCateDialogVisible = true;
      this.getParentCateList();
    },
    //添加分类
    addCate() {
      this.$refs.addCateFormRef.validate(async (valid) => {
        if (!valid) return;
        const data = await this.$http.post('categories', this.addCateForm);
        // console.log(data);
        if (data.status !== 200) return this.$message.error('提交标签失败');
        this.$message.success('提交成功');
        this.addCateDialogVisible = false;
        this.getCateList();
      });
    },
    //父级分类的数据列表
    async getParentCateList() {
      const data = await this.$http.get("categories", { params: { type: 2 } });
      console.log(data);
      if (data.status !== 200)
        return this.$message.error("获取父级分类数据失败");
      this.parentCateList = data.data.data;
    },
    //选择项发生
    parentCateChange() {
      // console.log(this.selectedKeys);
      //如果selectedKeys length> 0证明选中父级分类
      if (this.selectedKeys.length > 0) {
        //父级分类的id
        this.addCateForm.cat_pid = this.selectedKeys[
          this.selectedKeys.length - 1
        ];
        //为当前分类等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length;
      }
      this.addCateForm.cat_pid = 0;
      this.addCateForm.cat_level = 0;
    },
    //重置框
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields();
      this.selectedKeys = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_pid = 0;
      this.addCateDialogVisible = false;
    },
  },
};
</script>

<style lang="stylus" scoped>
.el-card {
  margin-top: 15px;

  .rolelist {
    margin-top: 15px;

    .el-pagination {
      margin-top: 15px;
    }
  }
}
</style>